package ltd.smallwang.springdemos.controller;

import jakarta.servlet.ServletInputStream;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

  @PostMapping("/test/cache-req-body")
  public String testCacheReqBody(@RequestBody String reqBody, HttpServletRequest httpServletRequest) {
    System.out.println(reqBody.length());
    try (ServletInputStream inputStream = httpServletRequest.getInputStream()) {
      System.out.println(IOUtils.toString(inputStream, StandardCharsets.UTF_8).length());
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return "ok";
  }
}
